package com.example.owner.lettery1.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.bumptech.glide.Glide;
import com.example.owner.lettery1.R;
import com.example.owner.lettery1.base.BaseFragment;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeFragment extends BaseFragment implements OnBannerListener,AdapterView.OnItemClickListener {
    //数据声明
    @BindView(R.id.banner)
    Banner banner;
    Context mContext;
    Unbinder unbinder;
    private    String feilei;
    private   String url;
    private GridView grid;
    private List<Integer> bannerlist = new ArrayList<>();
    private List<Map<String, Object>> data_list = new ArrayList<>();
    private int[] icons = {R.drawable.join_k3,R.drawable.join_dlt,R.drawable.join_ssq,R.drawable.join_xysc,
            R.drawable.join_pl3,R.drawable.join_pl5,R.drawable.join_gd11x5,R.drawable.join_qlc,R.drawable.join_fc3d};
    private String[] text = {"快3","大乐透","双色球","幸运赛车","排列3","排列5","11选5","七乐彩","3D"};



    public static HomeFragment newInstance() {

        return new HomeFragment();
    }

    @Override
    protected void onInitView(View view, Bundle savedInstanceState) {

        bannerlist.add(R.drawable.banner1);
        bannerlist.add(R.drawable.banner4);
        bannerlist.add(R.drawable.banner8);
        banner.setOnBannerListener(this);
        banner.setImages(bannerlist);
        //设置图片加载器
        banner.setImageLoader(new MyLoader());
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播间隔时间
        banner.setDelayTime(1000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //开启轮播图
                .start();

        //加载适配器
        grid = view.findViewById(R.id.grid);
        String[] form = {"image", "text"};
        int[] to = {R.id.image, R.id.text};
        //加载gridView数据
        getData();

        grid.setAdapter(new SimpleAdapter(getActivity(), data_list, R.layout.item_grid, form, to));
        grid.setOnItemClickListener(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
    //轮播图点击事件
    @Override
    public void OnBannerClick(int position) {
        switch (position){
            case 0:
                startActivity(position,BannerActivity.class);
                break;
            case 1:
                startActivity(position,Banner1Activity.class);
                break;
            case 3:
                startActivity(position,BannerActivity.class);
                break;
        }
    }

    private void startActivity(int position,Class mClass){
        Intent intent = new Intent(getActivity(),mClass);
        startActivity(intent);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /**
         * 竞猜足球修改为快三
         */
        /*竞猜足球","大乐透","双色球","幸运赛车","排列3","排列5","11选5","七乐彩","3D*/
        switch (position) {
            case 0:
                url="https://m.aicai.com/zst/xk3/hmyl.do?vt=5&clientType=0";
                starttoWeb(url);
                break;
            case 1:
                feilei="dlt";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 2:
                feilei="ssq";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 3:
                feilei="xysc";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 4:
                feilei="pls";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 5:
                feilei="plw";
                url="http://m.500.com/datachart/plw/zx/0.html";
                starttoWeb(url);
                break;
            case 6:
                feilei="jssyxw";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 7:
                feilei="qlc";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            case 8:
                feilei="sd";
                url="http://m.500.com/datachart/"+feilei+"/jb.html";
                starttoWeb(url);
                break;
            default:
                break;
        }
    }
        //抽取加载网页数据的方法
    private void starttoWeb(String url) {
        Intent intent= new Intent(getActivity(), ShowNumActivity.class);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }


    //自定义的图片加载器
    private class MyLoader extends ImageLoader {

        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((int) path).into(imageView);
        }
    }

    //准备数据gridView
    public List<Map<String, Object>> getData() {

        for (int i = 0; i < icons.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icons[i]);
            map.put("text", text[i]);
            data_list.add(map);
        }
        return data_list;
    }


}
